﻿using System;

namespace Microsoft.Popfly.GameCreator.GameEngine.Events
{
    public class RegularIntervalChecker : IEventChecker
    {
        private Game m_game;
        private double m_intervalInSeconds;
        private double m_totalElapsedTimeInSeconds;

        public RegularIntervalChecker(Game game, double intervalInSeconds)
        {
            m_game = game;
            m_intervalInSeconds = intervalInSeconds;
            m_totalElapsedTimeInSeconds = 0;
        }

        #region IEventChecker Members

        public bool FireThisFrame(double elapsedTimeInSeconds, out BehaviorOwner sender, out PGCEventArgs args)
        {
            m_totalElapsedTimeInSeconds += elapsedTimeInSeconds;

            if (m_totalElapsedTimeInSeconds >= m_intervalInSeconds)
            {
                sender = m_game.CurrentScene;
                args = null;
                
                m_totalElapsedTimeInSeconds -= m_intervalInSeconds;

                return true;
            }
            else
            {
                sender = null;
                args = null;

                return false;
            }
        }

        public bool IsOfEventType(string eventType)
        {
            return eventType == "Timer" || eventType == "timer";
        }

        #endregion
    }
}
